package com.gzy.studentsociety.dao;

import com.gzy.studentsociety.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.sun.xml.internal.ws.encoding.xml.XMLMessage.getDataSource;

public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public Student login(String acc, String pwd) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = " select * from student where studentName = ? and password = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),acc,pwd);
    }

    @Override
    public boolean register(Student student) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "INSERT INTO student (studentName, studentNumber, gender, birthday, password, graduated, avatarPath) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?)";
        // 使用 BeanHandler 对象来存储学生信息
        int result = qr.update(sql, student.getStudentName(), student.getStudentNumber(), student.getGender(),
                student.getBirthday(), student.getPassword(), student.getGraduated(), student.getAvatarPath());
        // 如果返回值大于 0，表示插入成功
        return result > 0;
    }

    @Override
    public Student findByNumber(String number) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentNumber = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),number);
    }

    @Override
    public Student findById(Integer id) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentId = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),id);
    }

}



